package library.http;


import java.io.File;

import library.http.progress.HttpDownloadListener;
import library.http.progress.ProgressPublisher;


public class Download<T> {
    private final T data;
    private HttpDownloadListener progress;
    private long rateProgress;
    private long rateSpeed;
    private File target;
    private long rangeStart = 0;
    private long rangeEnd = 0;
    private boolean overwrite;

    public static <T> Download<T> create() {
        return new Download<T>(null);
    }

    public static <T> Download<T> create(T data) {
        return new Download<T>(data);
    }

    private Download(T data) {
        this.data = data;
    }

    public Download<T> target(File target) {
        this.target = target;
        return this;
    }

    public Download<T> range(long rangeStart) {
        this.rangeStart = rangeStart;
        return this;
    }

    public Download<T> range(long rangeStart, long rangeEnd) {
        this.rangeStart = rangeStart;
        this.rangeEnd = rangeEnd;
        return this;
    }

    public Download<T> progress(HttpDownloadListener<File> progress) {
        this.progress = progress;
        return this;
    }

    public Download<T> overwrite(boolean overwrite) {
        this.overwrite = overwrite;
        return this;
    }
    /**
     * @param progress
     * @param rateProgress 监测进度时间间隔
     * @param rateSpeed    计算速率时间间隔,影响速率值，间隔越大速率值越平滑
     * @return
     */
    public Download<T> progress(HttpDownloadListener<T> progress, long rateProgress, long rateSpeed) {
        this.progress = progress;
        this.rateProgress = rateProgress;
        this.rateSpeed = rateSpeed;
        return this;
    }

    protected HttpDownloadListener getProgress() {
        return progress;
    }

    protected File getTarget() {
        return target;
    }

    protected long getRangeStart() {
        return rangeStart;
    }

    protected long getRangeEnd() {
        return rangeEnd;
    }

    protected boolean isOverwrite() {
        return overwrite;
    }

    protected ProgressPublisher crateProgressPublisher() {
        return new ProgressPublisher(data, progress, rangeStart, rateProgress, rateSpeed);
    }
}
